package com.haier.a920changjingyanshidemo.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.haier.a920changjingyanshidemo.Base;
import com.haier.a920changjingyanshidemo.R;
import com.haier.a920changjingyanshidemo.activity.EnjoyHeatActivity;

/**
 * Created by wanghaixin on 17/8/21.
 */
public class EnjoyHeatKeTingFragment extends Fragment {

    public static String TAG = EnjoyHeatKeTingFragment.class.getSimpleName();
    private Context context;
    private FrameLayout heatContain;
    private AnimationDrawable background;

    @Override
    public void onAttach(Context context) {
        this.context = context;
        super.onAttach(context);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View inflate = inflater.inflate(R.layout.fragment_enjoyheat_keting, null, false);
        heatContain = ((FrameLayout) inflate.findViewById(R.id.heat_contain));
        background = (AnimationDrawable) heatContain.getBackground();
        background.start();
        View deviceView = inflate.findViewById(R.id.device_View);
        deviceView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), EnjoyHeatActivity.class);
                intent.putExtra("No", "26"); //初始化温度传递给下个页面
                intent.putExtra("title", "客厅");
                startActivityForResult(intent, 0);
            }
        });
        return inflate;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.e(TAG, "onActivityResult: resultCode:" + resultCode);

        if (requestCode == 0) {
            //TODO 处理返回后的温度设置 及 页面的图片设置
            if (Base.isOpenDiNuan) {
                heatContain.setBackgroundResource(R.drawable.animation_1);
                if (!background.isRunning()) {
                    background = (AnimationDrawable) heatContain.getBackground();
                    background.start();
                }
                Log.e(TAG, "onActivityResult: " + Base.isOpenDiNuan);
            } else {
                heatContain.setBackgroundResource(R.mipmap.heat_keting_off);
                Log.e(TAG, "onActivityResult: " + Base.isOpenDiNuan);
                background.stop();
            }
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }
}
